<?php
/**
 * 首页相关应用
 * @author wangtao
 * @version 1.0 
 */

/**
 * 显示所以
 * @param int $num		显示数目
 * @param int $start	开始数
 * @return void
 */
function index($username = '', $num = '', $start = '')
{
	#初始化模板
	$views = new smasrty();
	$db = mysqlDB::con();
	$newPostSQL = 'SELECT `Pid`, `Post_Title`, `Post_Link`, left( `Post_Content` , 512 ) AS `Post_Content` 
	FROM `post_into` ORDER BY `post_into`.`Pid` DESC LIMIT 0 , 20';
	$newPost = $db->fetchAssoc($newPostSQL);
	foreach ($newPost as $key=>$post)
	{
		$newPost[$key]['Post_Content'] = mb_substr(strip_tags($post['Post_Content']), 0, 140, 'utf-8');
	}
	$DBsize = round($db->getTableLegth('post_into'), 3);
	
	$title = '我的订阅';
	
	#查询信息
	$querynum = $db->querynum;											//查询次数
	$queryTime = round(number_format(microtime(true) - $db->queryTime, 6), 3);	//查询所用时间
	$memory = round(memory_get_usage() / 1048576, 3);								//所用内存
	
	#$views -> assign('ipNum', $ipNum);
	$views -> assign('querynum', $querynum);
	$views -> assign('queryTime', $queryTime);
	$views -> assign('memory', $memory);
	$views -> assign('title', $title);
	$views -> assign('newPost', $newPost);
	$views -> assign('DBsize', $DBsize);
	$views -> assign('page', $newPost[0]['Pid']);
	
	$views -> display('index.html');
}

/**
 * 下一页
 * @param int $page
 */
function nextPage($page = '')
{
	if (empty($page))
	{
		die('error');
	}
	$sql = 'SELECT `Pid` , `Post_Title` , `Post_Link` , left( `Post_Content` , 512 ) AS `Post_Content`
	FROM `post_into`
	WHERE `Pid` <' . $page . ' ORDER BY `Pid` DESC LIMIT 20';
	$db = mysqlDB::con();
	$result = $db->query($sql);
	while ($row = $db->assoc($result))
	{
		echo '<div class="newposten">
				<h3><a href="/readOne/' . $row['Pid'] . '">' . $row['Post_Title'] . '</a></h3>
    			<p>' . mb_substr(strip_tags($row['Post_Content']), 0, 140, 'utf-8') . '<span>……</span></p>
    			<small>' . $row['Post_Link'] . '</small>
   			</div>';
	}
}

/**
 * 读取源下所有日志
 * @param int $BID		博客ID
 * @param int $start    读取列表开始
 * @param int $num      读取列表结束
 */
function readRSS($BID, $start = 'index', $num = 10)
{
	//header('HTTP/1.1 503 Service Unavailable');exit;
	$BID = str_replace('\'', '', $BID);
	$sql0 = 'SELECT `post_into`.`Pid` , `post_into`.`Post_Title` , `post_into`.`Post_PubDate` , `post_into`.`Post_Creator` , left( `Post_Content` , 512 ) AS `Post_Content`
	FROM `post_into`
	INNER JOIN `bid_pid_num` ON `post_into`.`Pid` = `bid_pid_num`.`pid`
	WHERE `bid_pid_num`.`bid` =' . $BID . '
	ORDER BY `bid_pid_num`.`num` DESC
	LIMIT 0 , 10';


	$views = new smasrty();
	$db = mysqlDB::con();
	$sql1 = 'SELECT `Blog_Title`, `countNUM` FROM `blog_info` WHERE `BID` = ' . $BID . ' LIMIT 0, 1';
	$array = $db->getRow($sql1);

	$title = $array['Blog_Title'];
	$conutNum = $array['countNUM'];

	$views -> assign('title', $title);
	$views -> assign('countNUM', $conutNum);
	#die($start);
	if ($start == 'index' || $start === '')
	{
		$cid = $BID . '-index';		#缓存ＩＤ
		$views->caching = 1;
		$views->cache_lifetime = -1;
		if (!$views->is_cached('main.html', $cid))
		{
			wt_post::creatCache($BID);
		}
	}
	else
	{
		$views->caching = 0;
		$views->cache_lifetime = -1;
		if ($start > wt_math::gTO0($conutNum))
		{
			die('<a href="' . WEBROOT . '">Back</a>');
		}
		$cid = $BID . '-' . $start;
		if (!$views->is_cached('main.html', $cid))
		{
			$sql = 'SELECT * FROM `post_into` WHERE `BID` =' . $BID . ' ';
			$stateStr = '&state';
			
			#生成URL
			$url = '/' . __FUNCTION__ . '/' . $BID;
			$page = http::rssPageF($conutNum, $start, $url);

			$post = array_reverse($db->selectLimit($sql, $num, $start));
			$views -> assign('post', $post);
			$views -> assign('pager', $page);
		}
	}
	$key = 'd';
	$views -> assign('key', $key);
	$views -> display('main.html', $cid);
}

/**
 * 读取某一篇日志
 * @param int $id	日志ID
 */
function readOne($id)
{
	$id = str_replace('\'', '', $id);
	#初始化模板  
	$views = new smasrty();
	$db = mysqlDB::con();
	$sqlFinal = 'SELECT `Pid` FROM `post_into` ORDER BY `post_into`.`Pid` DESC LIMIT 0 , 1';
	$sqlFirst = 'SELECT `Pid` FROM `post_into` ORDER BY `post_into`.`Pid` ASC LIMIT 0 , 1';
	$sql = 'SELECT * FROM `post_into` WHERE `Pid` = \'' . $id . '\'';
	$final = $db->query2one($sqlFinal);
	$first = $db->query2one($sqlFirst);
	if ($id == $final)
	{
		//		$pager = http::onePageF($first, $final, $id);
		$pager = http::onpageOF($id, $first, $final);
		$post = $db->fetch1line($sql);
		if (empty($post['Pid'])) {
			header('HTTP/1.0 404 Not Found');exit;
		}
		$views -> assign('title', $post['Post_Title']);
		$views -> assign('Post_Link', $post['Post_Link']);
		$views -> assign('Post_PubDate', $post['Post_PubDate']);
		$views -> assign('Post_Content', $post['Post_Content']);
		
		$views -> assign('pager', $pager);
		$views -> assign('Post_Creator', $post['Post_Creator']);
	}
	else
	{
		#$views->caching = 0;
		#$views->cache_lifetime = 0;
		if (!$views->is_cached('readpost.html', $id))
		{
			#$pager = http::onePageF($first, $final, $id);
			$pager = http::onpageOF($id, $first, $final);
			$post = $db->fetch1line($sql);
			if (empty($post['Pid'])) {
				header('HTTP/1.0 404 Not Found');exit;
			}
			$views -> assign('title', $post['Post_Title']);
			$views -> assign('Post_Link', $post['Post_Link']);
			$views -> assign('Post_PubDate', $post['Post_PubDate']);
			$views -> assign('Post_Content', $post['Post_Content']);

			$views -> assign('pager', $pager);
			$views -> assign('Post_Creator', $post['Post_Creator']);		
		}
	}
	$views->assign('DBsize', '');
	$views -> display('readpost.html');
	#$views -> display('readpost.html', $id);
}

/**
 * 显示当前作者文章
 * @param string $author
 * @param int    $num
 * @param int    $start
 * @retunr void
 */
function author($author, $start = 0, $num = 10)
{
	header('HTTP/1.1 503 Service Unavailable');exit;
	$db = mysqlDB::con();
	$sql = 'SELECT * FROM `post_into` WHERE `Post_Creator` = \'' . $author . '\' 
	ORDER BY `post_into`.`Post_PubDate` DESC';
	#获取总数据量（破方法）
	$conutNum = $db->queryNum($sql);
	$title = '搜索_' . $author;
	#生成URL
	$url = '/author/' . $author;
	$page = http::authorPageF($conutNum, $start, $url);
	#初始化模板
	$views = new smasrty();
	$db = mysqlDB::con();
	#页码转换
	if ($start == 1)
	{
		$start = 0;
	}
	$post = $db->selectLimit($sql, $num, $start);
	
	$views -> assign('post', $post);
	$views -> assign('title', $title);
	$views -> assign('pager', $page);
	$views -> assign('countNUM', $conutNum);
	
	$views -> display('main.html');
}

function readType($type)
{
}
